<template>
  <a-cascader :options="options"
              @change="onChange"
              :loadData="loadSubList"
              :placeholder="placeholder"
              :changeOnSelect="changeOnSelect"
              :disabled="disabled"
              :value="selectedValue"
              @popupVisibleChange="popupVisibleChange" />
</template>
<script>
import { postAction, restAction } from '@/api/manage'
import Vue from 'vue'
import { PARAMS } from '@/store/mutation-types'
export default {
  name: 'JAreaTreeTag',
  props: {
    value: {
      type: String,
      required: false
    },
    placeholder: {
      type: String,
      default: '请选择',
      required: false
    },
    top: {
      type: String,
      default: undefined,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    level: {
      type: Number,
      default: 5,
      required: false
    },
    canSelect: {
      type: Boolean,
      default: true,
      required: false
    },
  },
  data () {
    return {
      options: [
      ],
      defaultTop: this.top ? this.top : '100000',
      defaultCode: '100000',
      defaultValue: [],
      selectedValue: [],
      changeOnSelect: this.canSelect,
    }
  },
  created () {
    this.init();
  },
  watch: {
    value: function () {
      this.init()
    },
  },
  methods: {
    onChange (value) {
      this.selectedValue = value;
    },
    popupVisibleChange (dis) {
      if (dis == false) {
        this.$emit('change', this.selectedValue[this.selectedValue.length - 1]);
      }
    },
    init () {
      let self = this
      if(this.top){
        this.defaultTop = this.top
      }
      if (this.value) {
        restAction(`/sys/area/tree/${this.level}/${this.defaultTop}/${this.value}`).then(res => {
          if (res.success) {
            let retOptions = Object.assign([], res.result)
            self.options = retOptions.map(item => {
              let it = Object.assign({}, item);
              if(it.children === null){
                delete it.children
              }
              return it;
            })
            restAction(`/sys/area/detail/${this.defaultTop}/${this.value}`).then(resp => {
              this.selectedValue = resp.result
            })
          }
        })
      } else {
        this.selectedValue = [];
        restAction(`/sys/area/tree/${this.level}/${this.defaultTop}/${this.defaultTop}`, {}).then(resp => {
          if (resp.success) {
            let retOptions = Object.assign([], resp.result)
            self.options = retOptions.map(item => {
              let it = Object.assign({}, item);
              if(it.children === null){
                delete it.children
              }
              return it;
            })
          } else {
            console.error(resp.message)
          }
        });
      }
    },
    loadSubList (selectedOptions) {
      const targetOption = selectedOptions[selectedOptions.length - 1];
      targetOption.loading = true;

      postAction(`/sys/area/sublist/${this.level}/${targetOption.areaCode}`, {}).then(resp => {
        if (resp.success) {
          targetOption.loading = false;
          targetOption.children = resp.result;
          this.options = [...this.options];
        } else {
          console.error(resp.message)
        }
      }).finally(() => {
        targetOption.loading = false;
      })
    },
  },
  model: {
    prop: 'value',
    event: 'change'
  }
}
</script>